package com.xx.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {

	/**
	 * 全局统一处理500异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public R handleGlobalException(Exception e) {
		log.error("错误异常{}", e.getMessage());
		if(e instanceof BizException){
			return R.error(((BizException) e).getCode(), ((BizException) e).getMsg());
		}
		return R.error(500, e.getMessage());
	}
}